package com.mall.malladminbackend.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.malladminbackend.dto.SpecValueDto;
import com.mall.malladminbackend.entity.SpecValue;
import com.mall.malladminbackend.mapper.SpecValueMapper;
import com.mall.malladminbackend.model.PageBean;
import com.mall.malladminbackend.model.R;
import lombok.AllArgsConstructor;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;

/**
 * 作者：tk
 * 时间：2022/8/16 9:56
 * 描述：永无Bug!!!
 */
@Service
@AllArgsConstructor
public class SpecValueService {
    private SpecValueMapper specValueMapper;
    //表格渲染加分页
    public R selectSpecValue(Integer specId,Integer pageNum,Integer pageSize){
        Page<SpecValue> specValues = PageHelper.startPage(pageNum,pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("spec_id",specId);
        specValueMapper.selectList(qw);
        return R.buildSuccess(PageBean.buildPageBean(specValues));
    }
    //添加规格值
    public R insertSpecValue(SpecValueDto specValueDto) throws Exception {
        SpecValue specValue = new SpecValue();
        PropertyUtils.copyProperties(specValue,specValueDto);
        specValueMapper.insert(specValue);
        return R.buildSuccess();
    }
    //编辑
    public R updateSpecValue(SpecValueDto specValueDto) throws Exception {
        SpecValue specValue = new SpecValue();
        PropertyUtils.copyProperties(specValue,specValueDto);
        specValueMapper.updateById(specValue);
        return R.buildSuccess();
    }
    //规格值查询，数据回显
    public R selectSpecValueById(Integer id){

        return R.buildSuccess(specValueMapper.selectById(id));
    }
    //删除
    public R deleteSpecValueById(Integer id){
        specValueMapper.deleteById(id);
        return R.buildSuccess();
    }
}
